package com.xlink.action;

import com.xlink.dto.Sensor;
import com.xlink.repository.SensorRepository;
import com.xlink.service.SensorService;
import org.moon.base.action.BaseAction;
import org.moon.message.WebResponse;
import org.moon.rbac.domain.annotation.MenuMapping;
import org.moon.rest.annotation.Delete;
import org.moon.rest.annotation.Get;
import org.moon.rest.annotation.Post;
import org.moon.rest.annotation.Put;
import org.moon.utils.Maps;
import org.moon.utils.ParamUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by asus1 on 2015/12/22.
 */
@Controller("sensorAction")//控制器标示
@RequestMapping("/sensor")//映射路径转发,对于该类中的所有方法，访问时必须有/sensor前缀
public class SensorAction extends BaseAction {   //* 控制器，继承至`BaseAction`(`BaseAction`提供领域增强方式的便捷式调用)
    @Resource
    private SensorService sensorService;

    @Get//表示当前台请求/sensor时
    @MenuMapping(code = "sensor", name = "传感器信息", parentCode = "test", url = "/sensor")
    public ModelAndView showPage() {
        return new ModelAndView("pages/xlink/sensor");
    }
    //书写相关的业务逻辑方法,将领域模型相关的行为转发给领域处理，将服务类相关的逻辑转发给服务类处理
    @Get("/list/{deviceId}")   //查询
    @ResponseBody //这个注解是说返回一个json数据
    public  WebResponse list(HttpServletRequest request,@PathVariable("deviceId") Long deviceId){
        //获取参数
        Map<String,Object> params = ParamUtils.getAllParamMapFromRequest(request);
        params.put("deviceId",deviceId);
        return WebResponse.success(sensorService.listForPage(SensorRepository.class,"list",params));

       // Map<String,Object> params = ParamUtils.getAllParamMapFromRequest(request);
        //把参数传递到servcie层，在service层做实现。
        //return WebResponse.build().setResult(sensorService.listForPage(SensorRepository.class,"list",params));
    }

    @Get("/{sensorId}")   //查看单个传感器
    @ResponseBody
    public WebResponse get(@PathVariable("sensorId") Long sensorId)
    {return WebResponse.success(sensorService.get(sensorId));}

    //@Post   //添加
    @Post("/{deviceId}")
    @ResponseBody
    public WebResponse add(@PathVariable("deviceId") Long deviceId,HttpServletRequest request,
                           @RequestParam("type")String type,
                           @RequestParam("title")String title,
                           @RequestParam("tags")String tags,
                           @RequestParam(value = "unit",required = false)String unit,
                           @RequestParam(value = "formula",required = false)String formula,
                           @RequestParam("period")String period,
                           @RequestParam("time")String time,
                           @RequestParam("about")String about,
                           @RequestParam(value = "on_off",required = false,defaultValue = "0")int on_off) {
        Sensor sensor = new Sensor();
        sensor.setdeviceId(deviceId);
        sensor.settype(type);
        sensor.settitle(title);
        sensor.setTags(tags);
        sensor.setunit(unit);
        sensor.setformula(formula);
        sensor.setperiod(period);
        sensor.settime(time);
        sensor.setabout(about);
        sensor.seton_off(on_off);
        return WebResponse.success(sensorService.add(sensor));}

    @Put("/{sensorId}")   //修改
    @ResponseBody
    public WebResponse update(@PathVariable("sensorId")Long sensorId,@RequestBody Map<String,Object> params) {
        params.put("sensorId",sensorId);
        sensorService.update(params);
        return WebResponse.success();
    }

    @Delete("/{sensorId}")
    @ResponseBody
    public WebResponse delete(@PathVariable("sensorId")Long sensorId) {
        return WebResponse.build().setResult(sensorService.delete(sensorId));
    }

}
